﻿using System.Collections.Generic;
using XiaoweiVoiceAI.Diagnostics;

namespace XiaoweiV2
{
    public enum SessionType
    {
        Unknown,
        KeywordSpotting,
        SpeechRequest,
        RequestTTS,
        PromotionTTS,
        MoreResource,
        FetchAlarmList,
        ModifyAlarm,
        GetIotList
    }
    public class Session
    {
        private static readonly object m_lock = new object();
        public string VoiceId { get; private set; }
        public SessionType Type { get; private set; }

        public Session(SessionType type, string voiceId)
        {
            this.Type = type;
            this.VoiceId = voiceId;
        }

        //private static List<Session> sessions = new List<Session>();
        private static Dictionary<SessionType, string> sessions = new Dictionary<SessionType, string>();

        public static void AddSession(Session session)
        {
            Debug.WriteLine("lock wait:AddSession");
            lock (m_lock)
            {
                Debug.WriteLine("lock enter:AddSession");
                if (sessions.ContainsKey(session.Type))
                {
                    sessions.Remove(session.Type);
                }
                sessions.Add(session.Type, session.VoiceId);
            }
            
            //var s = GetSessionByType(session.Type);
            //if (s == null)
            //{
            //    sessions.Add(session);
            //}
            //else
            //{
            //    if(!s.VoiceId.Equals(session.VoiceId))
            //    {
            //        s.VoiceId = session.VoiceId;
            //    }
            //}
        }

        public static void DeleteSessionByType(SessionType type)
        {
            Debug.WriteLine("lock wait:DeleteSessionByType");
            lock (m_lock)
            {
                Debug.WriteLine("lock enter:DeleteSessionByType");
                if (sessions.ContainsKey(type))
                {
                    sessions.Remove(type);
                }
            }
        }

        public static Session GetSessionByVoiceId(string voice_id)
        {
            //foreach(var s in sessions)
            //{
            //    if(s.VoiceId == voice_id)
            //    {
            //        return s;
            //    }
            //}
            return null;
        }

        public static SessionType? GetTypeByVoiceId(string voice_id)
        {
            Debug.WriteLine("lock wait:GetTypeByVoiceId");
            lock (m_lock)
            {
                Debug.WriteLine("lock enter:GetTypeByVoiceId");
                foreach (var key in sessions.Keys)
                {
                    if (sessions[key] == voice_id)
                    {
                        return key;
                    }
                }
            }
                
            return null;
        }

        public static Session GetSessionByType(SessionType type)
        {
            //foreach (var s in sessions)
            //{
            //    if (s.Type == type)
            //    {
            //        return s;
            //    }
            //}
            return null;
        }
    }
}
